import { ajax } from '../lib/ajax.js';
import { tools } from '../lib/tools.js';

//#region 获取修改信息的部分
let txt_title = document.getElementById('txt_title');
let txt_source = document.getElementById('txt_source');
let txt_info = document.getElementById('txt_info');
let btnOk = document.getElementById('btnOk');

console.log(location.href);
console.log(location.search);
console.log(location.host);
let nid = location.search.replace('?nid=', '');
console.log('nid===>', nid);

ajax.get(`/crud/news/${nid}`, {}, (data) => {
  if (!data.success) {
    alert(data.message);
    return;
  }
  txt_title.value = data.data.title;
  txt_source.value = data.data.source;
  txt_info.value = data.data.info;
});

btnOk.addEventListener('click', () => {
  let news = {
    title: txt_title.value,
    source: txt_source.value,
    info: txt_info.value,
    nid: nid,
  };

  ajax.put('/crud/news',news, (data)=>{
    alert(data.message);
  })

});

//#endregion
